<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header-container">
				<image src="../../static/logo-d.png"></image>
			</view>
			<!-- <view class="title">
				海城市蚂蚁房产经济有限公司
			</view> -->
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="请输入用户名"
					:focus="isFocus"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="请输入密码"
				></wInput>
			</view>
			<wButton 
				class="wbutton"
				text="登 录"
				:rotate="isRotate" 
				@click="startLogin"
			></wButton>
			
			<!-- 其他登录 -->
			<!-- <view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view>
			</view> -->
			
			<!-- 底部信息 -->
			<!-- <view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view> -->
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	
	export default {
		data() {
			return {
				phoneData:'', //用户/电话
				passData:'', //密码
				isRotate: false, //是否加载旋转
				isFocus: true // 是否聚焦
			};
		},
		components:{
			wInput,
			wButton,
		},
		mounted() {
			this.isLogin();
		},
		methods: {
			isLogin(){
				// 判断缓存中是否登录过，直接登录
				try {
					const value = uni.getStorageSync('userInfo');
					if (value) {
						//有登录信息
						console.log("已登录用户：",value);
						// _this.$store.dispatch("setUserData",value); //存入状态
						uni.reLaunch({
							url: '/pages/dash/dash'
						});
					}
				} catch (e) {
					// error
				}
			},
		    startLogin(e){
				//登录
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '请输入用户名'
				    });
				    return;
				}
		        if (this.passData.length == "") {
		            uni.showToast({
		                icon: 'none',
						position: 'bottom',
		                title: '请输入密码'
		            });
		            return;
		        }
				
				this.$api.globalUtil.requestAjax({
					url: "/api/sysUser/login",
					method: "POST",
					data: {
						userName: this.phoneData,
						password: this.passData
					}
				}).then(res => {
					this.$api.msg("登录成功");
					uni.setStorageSync('operationId', res.data.objectId);
					const userData = {
						objectId: res.data.objectId,
						nickName: res.data.nickName,
						mobileNumber: res.data.mobileNumber,
						trueName: res.data.trueName
					};
					uni.setStorageSync('userInfo', userData);
					// this.$store.dispatch("setUserData",userData); //存入状态
					uni.reLaunch({
						url: '/pages/dash/dash'
					});
				});
		    }
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
	
	.title {
		text-align: center;
		font-size: 40rpx;
		margin-top: 150rpx;
		margin-bottom: 72rpx;
	}
	
	.header-container {
		text-align: center;
		margin-top: 100px;
		margin-bottom: 72rpx;
	}
	
	.header-container image {
		width: 101px;
		height: 135px;
	}
</style>
